package com.qf.controller;

import com.qf.entity.GoodsDetails;
import com.qf.entity.GoodsDetailsPage;
import com.qf.entity.GoodsPackage;
import com.qf.entity.PackageDetails;
import com.qf.service.GoodPackgeService;
import com.qf.service.GoodsDetailsService;
import com.qf.service.PackageDetailService;
import com.qf.utils.UploadUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class GoodsDetailsController {

    @Resource
    private GoodsDetailsService goodsDetailsService;

    @Resource
    private GoodPackgeService goodPackgeService;

    @Resource
    private PackageDetailService packageDetailService;

    @Resource
    private UploadUtils uploadUtils;

    private List<String> gdlist=null;

    @GetMapping("/findAllGoodDetails")
    public List<GoodsDetails> findAllGoodDetails(){
        return goodsDetailsService.findAllGoodDetails();
    }

    @GetMapping("/findAllGoodDetails/{pagesize}/{currentpage}")
    public GoodsDetailsPage findAllGoodPage(@PathVariable Integer pagesize,@PathVariable Integer currentpage){
        GoodsDetailsPage goodsDetailsPage = goodsDetailsService.getGoodsDetailsPage(currentpage, pagesize);
        System.out.println(goodsDetailsPage);
        return goodsDetailsPage;
    }

    @GetMapping("/deleteGoodsByGdid/{gdid}")
    public String deleteGoodsByGdid(@PathVariable Integer gdid){
        System.out.println(gdid);
        int i = goodsDetailsService.deleteGoodsByGdid(gdid);
        if(i==1){
            return "success";
        }
        return "fail";
    }

    @PostMapping("/updateGoodsByGdid")
    public String updateGoodsByGdid(@RequestBody GoodsDetails goodsDetails){
        try {
            goodsDetailsService.updateGoodsByGdid(goodsDetails);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }

    @PostMapping("/savaGoodsDetail")
    public String savaGoodsDetail(@RequestBody GoodsDetails goodsDetails){
        try {
            goodsDetailsService.saveGoodsDetails(goodsDetails);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    @GetMapping("/getGoodsDetailByGdid/{gdid}")
    public GoodsDetails getGoodsDetailByGdid(@PathVariable Integer gdid){
        GoodsDetails goodsDetails = goodsDetailsService.getGoodsDetailByGdid(gdid);
        System.out.println(goodsDetails);
        return goodsDetails;
    }

    @RequestMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename();
        System.out.println(originalFilename);
        return uploadUtils.upload(multipartFile);
    }

    @GetMapping("/findByGdnameGoods")
    public List<String> findByGdnameGoods(){
        List<String> gdnames = goodsDetailsService.findByGdnameGoods();
        return gdnames;
    }

    @PostMapping("/insertpack")
    public  String  insertpack(@RequestBody GoodsPackage goodsPackage){
        try {
            goodPackgeService.savaGoodPackge(goodsPackage);
            String join = String.join(",", gdlist);
            for (String s : gdlist) {
                int gdid = goodsDetailsService.getGoodDetailGdidByGdname(s);
                PackageDetails pd=new PackageDetails();
                pd.setGpid(goodsPackage.getGpid());
                pd.setGdid(gdid);
                pd.setGoodsname(join);
                pd.setPdnumber(1);
                packageDetailService.savaPackageDetails(pd);
            }
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            goodPackgeService.deleGoodPackge(goodsPackage);
            return "fail";
        }
    }

    @PostMapping("/insertpackdetail")
    public List<String> insertpackdetail(@RequestBody List<String> list){
        gdlist=list;
        return list;
    }
}
